using System.Collections.Generic;
using ProtoModels;
using UnityEngine;

public class SeasonTournamentLayer : MonoBehaviour
{
	public int energyCost = 10;

	public UISprite headerSprite;

	public UILabel titleLabel;

	public UILabel subtitleLabel;

	public UILabel subheaderLabel;

	public List<UISprite> stars = new List<UISprite>();

	public Color starOffColor;

	protected SeasonDefinitions.SeasonEvent activeEvent;

	public List<CompetitorCard> cards = new List<CompetitorCard>();

	public UILabel playButtonLabel;

	private PlayerDB.TournamentPlayer opponentDefinition;

	private Events.EventDefinition eventDefinition;

	public Events.EventStage.StageTier activeTier { get; private set; }

	public void ShowWithEvent(SeasonDefinitions.SeasonEvent activeEvent)
	{
		this.activeEvent = activeEvent;
		NavigationManager.instance.Push(base.gameObject);
	}

	public void OnEnable()
	{
		Init(activeEvent);
	}

	public void Init(SeasonDefinitions.SeasonEvent activeEvent)
	{
		TournamentCareerStats careerStatsManager = GGPlayerSettings.instance.careerStatsManager;
		TournamentCareerDAO.ActiveSeasonInterval.ActiveEvent activeEvent2 = careerStatsManager.GetActiveEvent(activeEvent.namedEventId);
		Debug.Log(" isFinished " + activeEvent2.isFinished);
		int stageIndex = activeEvent2.stageIndex;
		titleLabel.text = activeEvent.eventName;
		eventDefinition = EventsDB.instance.GetEventById(activeEvent.eventDefinitionId);
		Events.Tournament tournament = eventDefinition as Events.Tournament;
		if (tournament != null)
		{
			GameConstants.SetFlag(headerSprite, (int)activeEvent.country);
			Events.EventStage stage = tournament.GetStage(stageIndex);
			subheaderLabel.text = stage.stageName;
			SetStarsCount(tournament.starCount);
			TournamentCareerDAO.GenericEventStats statsForEvent = careerStatsManager.GetStatsForEvent(activeEvent.namedEventId);
			int wins = 0;
			if (statsForEvent != null)
			{
				wins = statsForEvent.numberOfWins;
			}
			activeTier = stage.GetActiveTier(wins);
			Events.PlayerId playerId = activeTier.eligibleOpponents[0];
			opponentDefinition = PlayerDB.instance.GetPlayer(playerId.playerDBIndex);
			CompetitorCard competitorCard = cards[0];
			CompetitorCard competitorCard2 = cards[1];
			GGPlayerSettings instance = GGPlayerSettings.instance;
			competitorCard.Init(instance.GetName(), instance.Model.flag, instance.statsManager.playerStats, opponentDefinition);
			competitorCard2.Init(opponentDefinition.playerName, (int)opponentDefinition.flag, opponentDefinition, instance.statsManager.playerStats);
		}
	}

	public void SetStarsCount(int starsCount)
	{
		int num = 0;
		foreach (UISprite star in stars)
		{
			Color color = ((num > starsCount) ? starOffColor : Color.white);
			star.color = color;
			num++;
		}
	}

	public void OnStartMatch()
	{
		if (BehaviourSingleton<EnergyManager>.instance.GetCurrentEnergyValue() < (float)energyCost)
		{
			TableTennisDialog.instance.ShowYesNo("Not Enough Energy", "Sorry, you don't have enough energy.", "Buy energy", "Cancel", delegate(bool complete)
			{
				NavigationManager.instance.Pop();
				if (complete)
				{
					TableTennisMainNavigation tableTennisMainNavigation = NavigationManager.instance as TableTennisMainNavigation;
					tableTennisMainNavigation.PushItemShop(StoreItemsConfig.StoreItem.ItemType.Food);
				}
			});
			return;
		}
		BehaviourSingleton<EnergyManager>.instance.SpendEnergy(energyCost);
		MatchParameters.MatchParams matchParams = MatchParameters.MatchParams.CreateDefault();
		GGPlayerSettings instance = GGPlayerSettings.instance;
		matchParams.player1Name = instance.GetName();
		matchParams.player1Flag = instance.Model.flag;
		matchParams.player2Name = opponentDefinition.playerName;
		matchParams.player2Flag = (int)opponentDefinition.flag;
		matchParams.seasonEvent = activeEvent;
		matchParams.pointsToWinGame = activeEvent.pointsToWinGame;
		matchParams.gamesToWinMatch = activeEvent.gamesToWinMatch;
		matchParams.opponentDefinition = opponentDefinition;
		matchParams.coinsToWin = activeTier.coinsForWin;
		BehaviourSingleton<ScreenNavigation>.instance.LoadTableTennisMatch(matchParams);
	}

	public void OnWinButtonPressed()
	{
		GGPlayerSettings.instance.careerStatsManager.AdvanceActiveEvent(activeEvent, TournamentCareerStats.EventScore.NotRanked, true);
		NavigationManager.instance.Pop();
	}

	public void OnLoseButtonPressed()
	{
		GGPlayerSettings.instance.careerStatsManager.AdvanceActiveEvent(activeEvent, TournamentCareerStats.EventScore.NotRanked, false);
		NavigationManager.instance.Pop();
	}

	private void Update()
	{
		if (CanPlay())
		{
			UITools.ChangeText(playButtonLabel, "Play");
		}
		else
		{
			UITools.ChangeText(playButtonLabel, "Unavailable");
		}
	}

	private bool CanPlay()
	{
		if (BehaviourSingleton<EnergyManager>.instance.GetCurrentEnergyValue() < (float)energyCost)
		{
			return false;
		}
		return true;
	}
}
